package com.justgame.project.service.just.query;

import java.util.List;
import java.util.ArrayList;
import java.util.Optional;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.justgame.project.cloud.common.query.Column;
import com.justgame.project.cloud.common.query.BaseSelectBuilder;
import com.justgame.project.cloud.common.query.AbstractQueryWrapper;
import com.justgame.project.cloud.common.query.Query;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.justgame.project.service.just.entity.JustGameCloud;
import com.justgame.project.service.just.query.JustGameInfoQuery;
import com.justgame.project.service.just.entity.JustGameInfo;
import com.justgame.project.service.system.query.SysUserInfoQuery;
import com.justgame.project.service.system.entity.SysUserInfo;
import com.justgame.project.service.just.wrapper.JustGameCloudWrapper;

public class JustGameCloudQuery extends AbstractQueryWrapper<JustGameCloud, MPJLambdaWrapper<JustGameCloud>, JustGameCloudWrapper> {

    private JustGameCloudQuery() {
        super(new MPJLambdaWrapper<>(JustGameCloud.class,"just_game_cloud"));
    }

    public static JustGameCloudQuery builder(){
        return new JustGameCloudQuery();
    }

    public static class Select{

        private final List<Query> queries;

        protected Select(){
            queries = new ArrayList<>();
        }

        protected Select(List<Query> queries) {
            this.queries = queries;
        }

        public Query[] getQueriesArray() {
            return queries.toArray(new Query[0]);
        }

        public static SelectBuilder builder(){
            return new SelectBuilder();
        }

        public Query[] build(){
            return queries.toArray(new Query[0]);
        }

        public static class SelectBuilder extends BaseSelectBuilder {
            public SelectBuilder JustGameCloud(){
                return JustGameCloud(Column.all());
            }

            public SelectBuilder JustGameCloud(Column column){
                return add(Query.builder(JustGameCloud.class)
                            .selectColumns(column)
                            .build());
            }

            public SelectBuilder createUser(){
                return createUser(Column.all());
            }

            public SelectBuilder createUser(Column column){
                return createUser(column, null);
            }

            public SelectBuilder createUser(Column column,SysUserInfoQuery.Select selectQuery){
                return createUser("createUserWrapper", column, null);
            }

            public SelectBuilder createUser(String mappingName,Column column,SysUserInfoQuery.Select selectQuery){
                return add(Query.builder(SysUserInfo.class)
                            .mapping(mappingName)
                            .leftOn((SFunction<SysUserInfo,?>)SysUserInfo::getId)
                            .rightOn((SFunction<JustGameCloud,?>)JustGameCloud::getCreateUser)
                            .selectColumns(column)
                            .join(Optional.ofNullable(selectQuery).map(SysUserInfoQuery.Select::getQueriesArray).orElse(null))
                            .build());
            }

            public SelectBuilder updateUser(){
                return updateUser(Column.all());
            }

            public SelectBuilder updateUser(Column column){
                return updateUser(column, null);
            }

            public SelectBuilder updateUser(Column column,SysUserInfoQuery.Select selectQuery){
                return updateUser("updateUserWrapper", column, null);
            }

            public SelectBuilder updateUser(String mappingName,Column column,SysUserInfoQuery.Select selectQuery){
                return add(Query.builder(SysUserInfo.class)
                            .mapping(mappingName)
                            .leftOn((SFunction<SysUserInfo,?>)SysUserInfo::getId)
                            .rightOn((SFunction<JustGameCloud,?>)JustGameCloud::getUpdateUser)
                            .selectColumns(column)
                            .join(Optional.ofNullable(selectQuery).map(SysUserInfoQuery.Select::getQueriesArray).orElse(null))
                            .build());
            }

            public SelectBuilder justGameInfo(){
                return justGameInfo(Column.all());
            }

            public SelectBuilder justGameInfo(Column column){
                return justGameInfo(column, null);
            }

            public SelectBuilder justGameInfo(Column column,JustGameInfoQuery.Select selectQuery){
                return justGameInfo("justGameInfoWrapper", column, null);
            }

            public SelectBuilder justGameInfo(String mappingName,Column column,JustGameInfoQuery.Select selectQuery){
                return add(Query.builder(JustGameInfo.class)
                            .mapping(mappingName)
                            .leftOn((SFunction<JustGameInfo,?>)JustGameInfo::getId)
                            .rightOn((SFunction<JustGameCloud,?>)JustGameCloud::getGameInfoId)
                            .selectColumns(column)
                            .join(Optional.ofNullable(selectQuery).map(JustGameInfoQuery.Select::getQueriesArray).orElse(null))
                            .build());
            }

            public Select build(){
                return new Select(getQueries());
            }
        }

    }
}